package com.whsxt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.Constants;
import com.whsxt.domain.Customer;
import com.whsxt.dto.CustomerDto;
import com.whsxt.exception.BusinessException;
import com.whsxt.mapper.CustomerMapper;
import com.whsxt.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询分页的客户数据
     * @param customerDto
     * @return
     */
    @Override
    public PageInfo<Customer> findByPage(CustomerDto customerDto) {
        // 开启分页
        Page<Object> objects = PageHelper.startPage(customerDto.getPage(), customerDto.getLimit());
        // 查询数据
        List<Customer> customerList = customerMapper.selectPage(customerDto);
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        pageInfo.setTotal(objects.getTotal());
        return pageInfo;
    }

    /**
     * 新增数据
     * @param customerDto
     * @return
     */
    @Override
    public boolean save(CustomerDto customerDto) {
        // 属性的校验
        validate(customerDto);
        // 属性的拷贝
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDto, customer);
        // 默认值的填充
        customer.setCreateTime(new Date());
        // 新增数据
        int row = customerMapper.insert(customer);
        return row > 0;
    }

    /**
     * 通过id集合删除数据
     * @param ids
     * @return
     */
    @Override
    public boolean batchRemove(List<Integer> ids) {
        return customerMapper.batchDelete(ids) > 0;
    }

    /**
     * 修改一辆车的数据
     * @param customerDto
     * @return
     */
    @Override
    public boolean update(CustomerDto customerDto) {
        // 先查询数据库要存在需要更新的数据
        Customer customer =  customerMapper.selectByPrimaryKey(customerDto.getId());
        if(customer == null){
            throw new IllegalArgumentException("要修改的数据不存在");
        }

        // 校验数据
        if(!customerDto.getIdCard().equals(customer.getIdCard())){
            throw new IllegalArgumentException("修改客户的证件号不一致");
        }

        // 数据的复制
        Customer update = new Customer();
        BeanUtils.copyProperties(customerDto, update);

        // 修改数据
        return customerMapper.updateByPrimaryKeySelective(update) > 0;
    }

    /**
     * 获取客户的集合数据
     * @return
     */
    @Override
    public List<Customer> selectAll() {
        CustomerDto query = new CustomerDto();
        return customerMapper.selectPage(query);
    }

    /**
     * 对数据的属性的校验
     * @param customerDto
     */
    private void validate(CustomerDto customerDto) {
        if(!StringUtils.hasText(customerDto.getIdCard())){
            throw new IllegalArgumentException("客户的证件号不能为空");
        }

        if(!StringUtils.hasText(customerDto.getPhone())){
            throw new IllegalArgumentException("客户的电话号码不能为空");
        }

        int count = customerMapper.selectCountByIdCard(customerDto.getIdCard());

        if(count > 0){
            throw new BusinessException(CodeMsg.CUSTOMER_ID_CARD_FOUND, customerDto.getIdCard());
        }
    }
}
